package com.alibaba.citrus.cr.sales.plan.facade.service.salesplan.publish;

import com.alibaba.citrus.cr.sales.plan.api.salesplan.publish.SalesPlanPublishQueryService;
import com.alibaba.citrus.cr.sales.plan.dto.api.request.PageQueryPlanScItemExtendRequest;
import com.alibaba.citrus.cr.sales.plan.sdo.SalesPlanScItemExtendSDO;
import com.alibaba.cz.base.tool.page.PageResult;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.bcorder.sales_plan.planscitemextend.dto.AddStockEditePlanScItemExtendRequest;
import com.epoch.app.bcorder.sales_plan.planscitemextend.dto.EditPlanScItemExtendRequest;
import com.epoch.app.bcorder.sales_plan.planscitemextend.service.PlanScItemExtendService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class PlanScItemExtendServiceImpl implements PlanScItemExtendService {

    @Autowired
    public SalesPlanPublishQueryService salesPlanPublishQueryService;

    @Autowired
    public PlanScItemExtendService planScItemExtendService;

    @Override
    public Result<Void> addStockEditePlanScItemExtend(AddStockEditePlanScItemExtendRequest addStockEditePlanScItemExtendRequest) {

        Result result = new Result();
        result.setSuccess(true);
        try {
            PageQueryPlanScItemExtendRequest pageQueryPlanScItemExtendRequest =  new PageQueryPlanScItemExtendRequest();
            BeanUtils.copyProperties(addStockEditePlanScItemExtendRequest,pageQueryPlanScItemExtendRequest);
            pageQueryPlanScItemExtendRequest.setLimit(1000);
            Result<PageResult<List<SalesPlanScItemExtendSDO>>> pageResultResult = salesPlanPublishQueryService.pageQueryPlanScItemExtendList(pageQueryPlanScItemExtendRequest);
            if (pageResultResult.getResult() != null && CollectionUtils.isNotEmpty(pageResultResult.getResult().getContent())) {
                for (SalesPlanScItemExtendSDO salesPlanScItemExtendSDO : pageResultResult.getResult().getContent()) {
                    int inventory = 0;
                    int returnCount = 0;
                    int firstCount = 0;
                    int middleCount = 0;
                    int lastCount = 0;
                    if (salesPlanScItemExtendSDO.getInventoryStr() != null) {
                        inventory = Integer.valueOf(salesPlanScItemExtendSDO.getInventoryStr());
                    }
                    firstCount = salesPlanScItemExtendSDO.getMonthFirstReturnCount() != null ? salesPlanScItemExtendSDO.getMonthFirstReturnCount().intValue() : 0;
                    middleCount = salesPlanScItemExtendSDO.getMonthMiddleReturnCount() != null ? salesPlanScItemExtendSDO.getMonthMiddleReturnCount().intValue() : 0;
                    lastCount = salesPlanScItemExtendSDO.getMonthLastReturnCount() != null ? salesPlanScItemExtendSDO.getMonthLastReturnCount().intValue() : 0;
                    returnCount = firstCount + middleCount + lastCount;

                    EditPlanScItemExtendRequest editPlanScItemExtendRequest = new EditPlanScItemExtendRequest();
                    editPlanScItemExtendRequest.setId(salesPlanScItemExtendSDO.getId());
                    editPlanScItemExtendRequest.setCompanyTaskCount(inventory + returnCount);
                    if (inventory != 0 || returnCount != 0) {
                        if (firstCount != 0) {
                            editPlanScItemExtendRequest.setMonthFirstCompanyTaskCount((firstCount * (inventory + returnCount)) / returnCount);
                        } else {
                            editPlanScItemExtendRequest.setMonthFirstCompanyTaskCount(0);
                        }

                        if (middleCount != 0) {
                            editPlanScItemExtendRequest.setMonthMiddleCompanyTaskCount((middleCount * (inventory + returnCount)) / returnCount);
                        } else {
                            editPlanScItemExtendRequest.setMonthMiddleCompanyTaskCount(0);
                        }

                        if (lastCount != 0) {
                            editPlanScItemExtendRequest.setMonthLastCompanyTaskCount((lastCount * (inventory + returnCount)) / returnCount);
                        } else {
                            editPlanScItemExtendRequest.setMonthLastCompanyTaskCount(0);
                        }
                    }
                    planScItemExtendService.editPlanScItemExtend(editPlanScItemExtendRequest);

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
        }
        return result;
    }
}
